package zhuzx.generator;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import org.zhuzx.util.JdbcUtil;
import org.zhuzx.util.ListDataUtil;
import org.zhuzx.util.MicroUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * @author zhuzx 2022/7/29
 */
public class GuiCodeGenerator extends Application {

	@Override
	public void start(Stage primaryStage) throws Exception {
		primaryStage.setTitle("代码生成器");
		Connection conn = JdbcUtil.getDbConn(null);
		List<String> allValidTableName = getAllValidTableName(conn);
		HBox root = new HBox();
		ObservableList<Node> areas = root.getChildren();
		Pane pane = new Pane();
		areas.add(buildDropdownList(allValidTableName, item -> initTableColumn(conn, item, pane)));
		areas.add(pane);
		primaryStage.setScene(new Scene(root));
		primaryStage.setMaximized(true);
		primaryStage.show();
	}

	private static <E> Region buildDropdownList(List<E> dataList, Consumer<E> listener) {
		ListView<E> listView = new ListView<>(FXCollections.observableArrayList(dataList));
//		listView.setPrefSize(800, 300);
		MultipleSelectionModel<E> selectionModel = listView.getSelectionModel();
		selectionModel.setSelectionMode(SelectionMode.MULTIPLE);
		if (listener != null) {
			selectionModel.selectedItemProperty().addListener(
					(observable, oldValue, newValue) -> listener.accept(newValue));
		}
		return listView;
	}

	private static List<String> getAllValidTableName(Connection conn) {
		List<String> list;
		try {
			String schema = conn.getCatalog();
			String sql = "SELECT table_name FROM information_schema.TABLES WHERE table_schema = '" +
					schema + "'";
			list = ListDataUtil.getValues(JdbcUtil.executeQuery(sql, conn), "table_name");
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		return list.stream().filter(item -> {
			String suffix = MicroUtil.subBehind(item, '_');
			return !suffix.matches("((\\d+)|(ba(k|ck|ckup))){1,2}");
		}).collect(Collectors.toList());
	}
	
	private static void initTableColumn(Connection conn, String tableName, Pane columnRegion) {
		List<DbColumnMetadata> columns = DatabaseMetadataUtil.getColumns(conn, tableName);
		List<HBox> rows = columns.stream().map(column -> {
			HBox row = new HBox();
			ObservableList<Node> cells = row.getChildren();
			Text ordinal = new Text(String.valueOf(column.ordinal));
			ordinal.setX(30);
			cells.add(ordinal);
			cells.add(new CheckBox());
			cells.add(new CheckBox());
			return row;
		}).collect(Collectors.toList());
		ListView<HBox> listView = new ListView<>(FXCollections.observableArrayList(rows));
		listView.setPrefSize(800, 800);
		columnRegion.getChildren().clear();
		columnRegion.getChildren().add(listView);
//		TableView
	}

}